Skip to content

fix: replace bare except with specific Exception handler in K8s client init#1300

Open
v0idheaven wants to merge 3 commits into
krkn-chaos:mainfrom
v0idheaven:fix/bare-except-k8s-client-init
Open

fix: replace bare except with specific Exception handler in K8s client init#1300
v0idheaven wants to merge 3 commits into
krkn-chaos:mainfrom
v0idheaven:fix/bare-except-k8s-client-init

Conversation

@v0idheaven
Copy link
Copy Markdown
Contributor

@v0idheaven v0idheaven commented May 12, 2026

Fixes #1298

The bare except: block in
un_kraken.py around line 231 was silently swallowing everything — including KeyboardInterrupt and SystemExit. Worse, if KrknKubernetes() failed before kubecli was assigned, the except block itself would throw a NameError that also disappeared silently. Bad kubeconfig, zero feedback, confusing crash later.

Replaced it with except Exception:, added logging.exception() with the kubeconfig path, and return 1 on failure. Also removed the no-op bare kubeconfig_path expression that was doing nothing.

Tested by pointing config at a nonexistent kubeconfig path — logs the error clearly and exits cleanly.

@qodo-code-review
Copy link
Copy Markdown

Review Summary by Qodo

Replace bare except with specific Exception handler in K8s client init

🐞 Bug fix

Grey Divider

Walkthroughs

Description
• Replace bare except clause with specific Exception handler
• Add clear error logging with kubeconfig path and exception details
• Return -1 to exit gracefully instead of continuing with undefined kubecli
• Remove no-op kubeconfig_path bare expression statement
Diagram
flowchart LR
  A["Bare except clause<br/>catches all exceptions"] -->|"Replace with"| B["except Exception as e"]
  B -->|"Log error with"| C["kubeconfig path<br/>and exception details"]
  C -->|"Exit gracefully"| D["return -1"]
  E["Remove no-op<br/>kubeconfig_path statement"] -->|"Cleanup"| D
Loading

Grey Divider

File Changes

1. run_kraken.py 🐞 Bug fix +7/-3

Fix bare except and improve K8s client error handling

• Replaced bare except: with except Exception as e: to catch only specific exceptions
• Added structured error logging with kubeconfig path and exception details
• Changed error handling to return -1 instead of calling undefined
 kubecli.initialize_clients(None)
• Removed no-op kubeconfig_path bare expression statement

run_kraken.py


Grey Divider

Qodo Logo

@qodo-code-review
Copy link
Copy Markdown

qodo-code-review Bot commented May 12, 2026

Code Review by Qodo

🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0)

Grey Divider


Action required

1. Exit log omits exit code ✓ Resolved 📘 Rule violation ◔ Observability
Description
The new failure return path logs an error but does not include the exit code value being returned,
violating the requirement to log the scenario exit reason including the exit code. This reduces
observability when debugging exits based on return codes.
Code

run_kraken.py[R231-236]

+            logging.error(
+                "Failed to initialize Kubernetes client with kubeconfig %s: %s",
+                kubeconfig_path,
+                e,
+            )
+            return -1
Evidence
The checklist requires logging both a reason and the exit code value before returning an exit code,
but the added log message does not mention the returned code (-1).

Rule 455884: Log scenario exit reason before returning exit code
run_kraken.py[231-236]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
A return path that yields an exit code must log the exit code value and a clear reason before returning.

## Issue Context
The exception handler logs a failure message but does not include the numeric exit code it returns.

## Fix Focus Areas
- run_kraken.py[231-236]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


2. main() returns -1 exit ✓ Resolved 📘 Rule violation ≡ Correctness
Description
The new Kubernetes init failure path returns -1, which is a non-standard exit code for scenario
failures and can map to unexpected process exit codes. This breaks the requirement that scenario
failure paths terminate with exit code 1.
Code

run_kraken.py[R231-236]

+            logging.error(
+                "Failed to initialize Kubernetes client with kubeconfig %s: %s",
+                kubeconfig_path,
+                e,
+            )
+            return -1
Evidence
The compliance checklist requires scenario failure paths to use exit code 1, but the newly added
failure return path uses return -1 after catching initialization errors.

Rule 455882: Scenario implementations must use standardized exit codes
run_kraken.py[231-236]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
`run_kraken.py:main()` returns `-1` on Kubernetes client initialization failure, but scenario implementations must use standardized exit codes (0 success, 1 failure).

## Issue Context
The new exception handler logs the failure and returns `-1`. Per compliance, failure paths should return/exit with code `1`.

## Fix Focus Areas
- run_kraken.py[231-236]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools



Remediation recommended

3. Missing init stack trace ✓ Resolved 🐞 Bug ◔ Observability
Description
The new Kubernetes client init exception handler logs only str(e) and drops the traceback, making
it difficult to diagnose initialization failures. Use logging.exception(...) or
logging.error(..., exc_info=True) so the stack trace is preserved in logs.
Code

run_kraken.py[R230-236]

+        except Exception as e:
+            logging.error(
+                "Failed to initialize Kubernetes client with kubeconfig %s: %s",
+                kubeconfig_path,
+                e,
+            )
+            return -1
Evidence
The new handler in run_kraken.py logs an error message but does not include traceback information.
Elsewhere in the repo, exception handlers use logging.exception(...) to capture tracebacks,
showing the expected pattern for debuggable error logs.

run_kraken.py[225-236]
krkn/utils/VirtChecker.py[172-188]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
`run_kraken.main()` catches `Exception` during Kubernetes client initialization but logs it with `logging.error(..., e)` without including the traceback. This removes the most useful debugging context for kubeconfig/auth/SSL/client init failures.

## Issue Context
The codebase already uses `logging.exception(...)` in other exception handlers to preserve traceback details.

## Fix Focus Areas
- run_kraken.py[225-236]

### Suggested change
Replace the `logging.error(...)` call with either:
- `logging.exception("Failed to initialize Kubernetes client with kubeconfig %s", kubeconfig_path)`

or:
- `logging.error("Failed ...", kubeconfig_path, e, exc_info=True)`

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

Qodo Logo

Comment thread run_kraken.py Outdated
Comment thread run_kraken.py Outdated
…t init

The bare except: clause in run_kraken.py silently swallowed all
exceptions during Kubernetes client initialization, including
KeyboardInterrupt and SystemExit. This made failures invisible and
caused confusing secondary errors (NameError on kubecli) later in
the run.

Changes:
- Remove bare except: and replace with except Exception as e:
- Log a clear error message with the kubeconfig path and exception
- Return -1 to exit gracefully instead of continuing with an
  undefined kubecli reference
- Remove the no-op kubeconfig_path bare expression statement

Fixes krkn-chaos#1298

Signed-off-by: v0idheaven <dahiyavarun2007@gmail.com>
- Switch to logging.exception() so the full stack trace is preserved
- Use return 1 instead of -1 to match standardized exit codes
- Log message now includes the exit code for observability

Signed-off-by: v0idheaven <dahiyavarun2007@gmail.com>
@v0idheaven v0idheaven force-pushed the fix/bare-except-k8s-client-init branch from 17b425e to 4f0f62c Compare May 12, 2026 14:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: bare except clause silently swallows errors during Kubernetes client initialization in run_kraken.py

1 participant